package com.wlf.template;

import com.wlf.constant.DateBuild;
import com.wlf.entity.ProjectAddress;
import com.wlf.util.PrintUtil;
import org.plutext.jaxb.xslfo.BackgroundRepeatType;
import org.plutext.jaxb.xslfo.Block;
import org.plutext.jaxb.xslfo.BorderStyleType;
import org.plutext.jaxb.xslfo.DisplayAlignType;
import org.plutext.jaxb.xslfo.Flow;
import org.plutext.jaxb.xslfo.Inline;
import org.plutext.jaxb.xslfo.LanguageType;
import org.plutext.jaxb.xslfo.LayoutMasterSet;
import org.plutext.jaxb.xslfo.Leader;
import org.plutext.jaxb.xslfo.PageNumber;
import org.plutext.jaxb.xslfo.PageNumberCitation;
import org.plutext.jaxb.xslfo.PageSequence;
import org.plutext.jaxb.xslfo.RegionAfter;
import org.plutext.jaxb.xslfo.RegionBefore;
import org.plutext.jaxb.xslfo.RegionBody;
import org.plutext.jaxb.xslfo.RegionEnd;
import org.plutext.jaxb.xslfo.RegionStart;
import org.plutext.jaxb.xslfo.Root;
import org.plutext.jaxb.xslfo.SimplePageMaster;
import org.plutext.jaxb.xslfo.StaticContent;
import org.plutext.jaxb.xslfo.Table;
import org.plutext.jaxb.xslfo.TableBody;
import org.plutext.jaxb.xslfo.TableCell;
import org.plutext.jaxb.xslfo.TableRow;
import org.plutext.jaxb.xslfo.TextAlignType;
import org.plutext.jaxb.xslfo.WhiteSpaceTreatmentType;
import org.plutext.jaxb.xslfo.WhiteSpaceType;
import org.plutext.jaxb.xslfo.WrapOptionType;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * PMI 增值服务授权书
 * @author ex_weilingfeng
 *
 */
@Component
public class VsaTemplate {



/**
 * 返回一个Root对象
 */
	public Root createRoot(){
		Root root = new Root();
		root.setFontFamily("SimSun"); //设置字体
		LayoutMasterSet layMas = new LayoutMasterSet();

		SimplePageMaster simplePageMaster =new SimplePageMaster();

		simplePageMaster=buildPageLayout(simplePageMaster);
		layMas.getSimplePageMasterOrPageSequenceMaster().add(simplePageMaster);
		root.setLayoutMasterSet(layMas);
		root.getPageSequence().add(createPageSequence(new PageSequence()));

		return root;
	}

	/**
	 * 构造页面布局
	 * @param simplePageMaster
	 * @return
	 */
	public SimplePageMaster buildPageLayout(SimplePageMaster simplePageMaster) {

		simplePageMaster.setMasterName("healthy");
		simplePageMaster.setPageWidth("210mm");//页宽
		simplePageMaster.setPageHeight("300mm");//页高
		simplePageMaster.setMarginTop("4mm");//页上边距
		simplePageMaster.setMarginBottom("4mm");//页下边距
		simplePageMaster.setMarginLeft("8mm");//左边距
		simplePageMaster.setMarginRight("8mm");//右边距

		RegionBody  regionBody = new RegionBody();
		regionBody.setBackgroundRepeat(BackgroundRepeatType.NO_REPEAT); //设置不平铺
		regionBody.setBackgroundImage("http://lingfeng.shop/yaoyao/DRAFT.jpg");
		regionBody.setBackgroundPositionHorizontal("50%");//水平居中
		regionBody.setBackgroundPositionVertical("50%");//竖直居中
		regionBody.setMarginTop("2.5cm");//上边距
		regionBody.setMarginBottom("3cm");//页底部边距
		regionBody.setMarginLeft("15pt");//左边距
		regionBody.setMarginRight("15pt");//右边距

		simplePageMaster.setRegionBody(regionBody);

		RegionBefore rbefore = new RegionBefore();
		rbefore.setExtent("1.6cm"); //页眉
		simplePageMaster.setRegionBefore(rbefore);

		RegionAfter rafter = new RegionAfter();
		rafter.setExtent("2.5cm");//页脚
		simplePageMaster.setRegionAfter(rafter);

		RegionStart rstart = new RegionStart();
		rstart.setExtent("15pt");//左侧栏
		simplePageMaster.setRegionStart(rstart);

		RegionEnd rend = new RegionEnd();
		rend.setExtent("15pt");
		simplePageMaster.setRegionEnd(rend);

		return simplePageMaster;
	}

	/**
	 * 组装page-sequence对象
	 * @throws Exception
	 */
	public PageSequence createPageSequence(PageSequence pageSequence){
		pageSequence.setMasterReference("healthy");
		for(int i =0;i<2 ;i++){
			StaticContent  staticContent = new StaticContent();
			if(i==0){
				pageSequence.getStaticContent().add(pageHeader(staticContent));
			}else if(i==1){
				pageSequence.getStaticContent().add(pageFooter(staticContent));
			}
		}
		Flow flow = new Flow();
		pageSequence.setFlow(makeFirstFlow(flow));

		return pageSequence;
	}

	//页眉
	 public StaticContent pageHeader(StaticContent content){

		 content.setFlowName("xsl-region-before");
		 Block block =new Block();
		 block.setFontSize("8pt");
		 //block.getContent().add("这里是页眉");
		 content.getBlockOrBlockContainerOrTable().add(block);
		 return content;
	  }

	// 页脚
	public StaticContent pageFooter(StaticContent content){
		Block block;
		content.setFlowName("xsl-region-after");


		block =new Block();
		block.setFontSize("8pt");
		//block.getContent().add("这里是页脚");
		block.setTextAlign(TextAlignType.END);
		content.getBlockOrBlockContainerOrTable().add(block);



		block = new Block();
		block.setTextAlign(TextAlignType.END);
		block.setFontSize("10.5pt");
		block.setSpaceBefore("15pt");
		block.setMarginTop("1cm");
		PageNumber pageNum = new PageNumber();
		block.getContent().add(pageNum);
		block.getContent().add("页");
		block.getContent().add("\r\n/");
		PageNumberCitation pagNmCat = new PageNumberCitation();
		pagNmCat.setRefId("TheVaryLastPage");
		block.getContent().add(pagNmCat);
		block.getContent().add("页");
		content.getBlockOrBlockContainerOrTable().add(block);

		return content;
	}

	public  Flow makeFirstFlow(Flow flow){

		flow.setFlowName("xsl-region-body");
		Block block;
		Inline inline;

		block = new Block();
		block.getContent().add("增值服务授权书");
		block.setFontFamily("SongHei");
		block.setFontSize("20pt");
		block.setTextAlign(TextAlignType.CENTER);
		flow.getMarkerOrBlockOrBlockContainer().add(block);

		block =new Block();
		block.setFontFamily("SimSun");
		block.setFontSize("15pt");
		block.setSpaceBefore("50pt");
		block.setWhiteSpace(WhiteSpaceType.PRE);
		block.setWrapOption(WrapOptionType.WRAP);
		block.setLanguage(LanguageType.ZH);
		block.setLineHeight("30pt");

		block.getContent().add("本人           危凌峰                授权    上海天地汇物理上海天地汇物流管理有限公司        将业务款项结算给         苗瑞林   人");
		flow.getMarkerOrBlockOrBlockContainer().add(block);

		block =new Block();
		block.setFontFamily("SimSun");
		block.setFontSize("15pt");
		block.getContent().add("签署日期："+new Date());
		block.setSpaceBefore("30pt");
		flow.getMarkerOrBlockOrBlockContainer().add(block);

		Leader leader=PrintUtil.drawLine();
		block =new Block();
		block.getContent().add(leader);
		block.setLineHeight("50pt");
		flow.getMarkerOrBlockOrBlockContainer().add(block);

		//页码
		block=new Block();
		block.setId("TheVaryLastPage");
		flow.getMarkerOrBlockOrBlockContainer().add(block);

		return flow;
	}

}
